﻿using Auto.AlgsAndPush.DTO;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Auto.AlgsAndPush.Services.DeviceType
{
    public abstract class BaseDeviceType
    {
        private readonly HttpClientService _httpClientService;

        public BaseDeviceType(HttpClientService httpClientService)
        {
            _httpClientService = httpClientService;
        }
        /// <summary>
        /// 拉取多期的成果数据
        /// </summary>
        /// <param name="prjId">测组Id</param>
        /// <param name="startTime">起始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns></returns>
        public abstract Task<List<OriginResult>> PullMultiNumberResultsAsync(int prjId, DateTime startTime, DateTime endTime);

        /// <summary>
        /// 将滤波后的数据推送回原成果数据库
        /// </summary>
        /// <param name="prjId">测组Id</param>
        /// <param name="filterResult"></param>
        /// <returns></returns>
        public abstract Task<bool> PushFilterResultAsync(int prjId, FilterResult filterResult);

        public virtual async Task<List<OriginResult>> GetOriginResultsAsync(string url)
        {
            var response = await _httpClientService.GetAsync(url);
            var contentStr = await response.Content.ReadAsStringAsync();
            var list = JsonConvert.DeserializeObject<IEnumerable<OriginResult>>(contentStr);
            //避免在重新计算的时候 拉取到上一次的滤波数据
            return list.Where(x => !(x.IsFilter.HasValue && (bool)x.IsFilter)).ToList();
        }

        public virtual async Task<bool> UploadFilterResult(string url, FilterResult filterResult)
        {
            var response = await _httpClientService.PostAsync(url, filterResult);
            var ret = await response.Content.ReadAsStringAsync();
            return bool.Parse(ret);
        }

    }
}
